import { Application } from "egg";
import { ObjectId } from "mongoose";
import * as AutoIncrementFactory from "mongoose-sequence";
export interface RoleProps {
  id?: number;
  role_name: string; // 角色名称
  company_id: ObjectId; // 所属公司
  role_group_id: ObjectId; // 所属分组ID
  privileges_id: [];
  is_delete: boolean; // 是否删除
}
function initRoleModel(app: Application) {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;
  const AutoIncrement = AutoIncrementFactory(mongoose);
  const RoleScheme = new Schema<RoleProps>(
    {
      role_name: { type: String, unique: true, required: true },
      company_id: {
        type: Schema.Types.ObjectId,
        ref: "Company",
        required: true,
      },
      role_group_id: {
        type: Schema.Types.ObjectId,
        ref: "RoleGroup",
        required: true,
      },
      privileges_id: { type: Array, ref: "" },
      is_delete: { type: Boolean, default: false },
    },
    {
      timestamps: true,
      toJSON: {
        transform(_doc, ret) {
          delete ret.__v;
        },
      },
    }
  );
  RoleScheme.plugin(AutoIncrement, {
    inc_field: "id",
    id: "Role_counter",
  });
  return mongoose.model<RoleProps>("Role", RoleScheme);
}

export default initRoleModel;
